<?php
      /**
    * @name 短信接口发送类
    * @copyright tcsos inc
    * @version cooperation v1.0
    */
    class sms{
        
        private $api ;
        private $acc ;
        private $pwd ;
        
        private $api_type;  //接口类型，是哪种接口
        
        private $config;
        
        
         //构造函数
        public function __construct($sms_config)   
        {
        	global $_site;
            if(!$sms_config || empty($sms_config)){
                exit('sms class exit.');
            }
            $this->config = $sms_config[$_site->site_config['site']['sms_using']];
            
            if($_site->site_config['site']['sms_using'] != 'down119'){
                $this->api = $this->config['api'];
                $this->acc = $this->config['acc'];
                $this->pwd = $this->config['pwd'];
            }
        }
        //析构函数
        public function __destruct(){

        }
        
        /**
        * 发送短信
        * 
        * @param int $mobile        手机号
        * @param string $content    内容
        * @param string $charset    编码
        * @param string $mobile_prefix  手机前缀
        * @return bool
        */
        public function sms_send($mobile , $content , $charset = 'gb2312', $mobile_prefix = ''){
        	global $_site;
            if(!is_mobile($mobile) || !$content){
                return false;
            }
            
            switch($_site->site_config['site']['sms_using']){
                case 'down119': //掌上搜正式接口
                    return $this->_soap_send($mobile, $content);
                    break;
                case '180': //电信接口
                case 'sdk': //小区通原接口，四川的         
                    $acc = $this->config['acc'];
                    $pwd = $this->config['pwd'];
                    $api = $this->config['api'];
                    break;
                default:
                    return false;
            }
            $msg = $content;
            
            $url = str_replace('{acc}',$acc,$api);
            $url = str_replace('{pwd}',$pwd,$url);
            $url = str_replace('{tel}',$mobile,$url);
            $url = str_replace('{msg}',$msg,$url);
            $url = str_replace(' ','',$url);
            $url = iconv('utf-8',$charset,$url);
            
            $flag = file_get_contents($url);
            if($flag == '0'){
                return true;
            }
            //三网通的接口返回的是xml格式
            if(strpos($flag, '<?xml') !== false){
                //这里简单处理，只提取状态码
                $match = array();
                preg_match('/\<response\>(-?\d)+\<\/response\>/',$flag, $match);
                if($match[1] > 0){
                    return true;
                }
            }
            return false;
        }
        
        /**
        * SOAP方式改善短信
        * @author zhufu 2013-09-19
        * 
        * @param string $mobile 手机号
        * @param string $content 内容
        */
        private function _soap_send($mobile, $content){
            $ext_loaded =extension_loaded('soap');
            if(!$ext_loaded){
                return false;
            }
            $client = new SoapClient($this->config['client']);
            $param = $this->config['parameters'];
            $param[$this->config['mobile_key']] = $mobile . ',';
            $param[$this->config['content_key']] = $content . $this->config['sign'];
          $p = $client->__soapCall($this->config['function'], array('parameters'=>$param));
            if($p->SendMegResult == $this->config['success_tag']){
                return true;
            }
            return false;            
        }
        
    }
?>